//package com.paypalpay.timingtask;
//
//import com.paypalpay.entity.common.UserboundPaymentOrder;
//import com.paypalpay.pojo.Constant;
//import com.paypalpay.pojo.request.RequestBody.PayNotifyRequestBody;
//import com.paypalpay.pojo.request.RequestHeader;
//import com.paypalpay.pojo.request.subRequest.PayNotifyRequest;
//import com.paypalpay.pojo.response.subResponse.PayNotifyResponse;
//import com.paypalpay.service.*;
//import com.paypalpay.turntointerfaceservice.utils.TurntoInterfaceUtils;
//import com.paypalpay.util.JaxbUtil;
//import com.paypalpay.util.wx.HttpClientTool;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import javax.servlet.http.HttpSession;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//
///**
// * Demo 微信无卡用户定时去北京传支付结果通知的定时任务类
// *
// * @author lm
// * @date 2018/09/9
// */
//@Configuration
//@EnableScheduling
//public class NotificationNoDeviceTask {
//
//    @Autowired
//    private HttpSession session;
//
//    @Autowired
//    private SequenceService sequenceService;
//
//    @Autowired
//    private UserboundPaymentOrderService userboundPaymentOrderService;
//
//    @Autowired
//    private InternationalPayPackageService internationalPayPackageService;
//
//    @Autowired
//    private UnifiedOrderPackagesService unifiedOrderPackagesService;
//
//    @Autowired
//    PartnerService partnerService;
//
//    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//    String time = df.format(new Date());
//
//    //@Scheduled(cron = "0 5 0/2 * * ?")//每两个小时的整点过5分钟执行
//    public void notificationTimingTask() {
//        try {
//            System.out.println("NotificationNoDeviceTask微信无卡用户批量支付结果通知定时任务正在执行！");
//            //去数据库查询一下有哪些记录是需要批量回传支付结果通知的
//            List<UserboundPaymentOrder> userList = userboundPaymentOrderService.getNocardToNotifyList();
//            if (userList == null || userList.isEmpty()) {
//                System.out.println("没有查询到需要去做支付结果通知的数据！");
//            } else {
//                for (UserboundPaymentOrder userOrder : userList) {
//                    String partnerCode = userOrder.getPartnerCode();
//                    String secret = partnerService.findSecretByPartnerCode(partnerCode);
//                    String orderId = userOrder.getOrderId();
//                    String deviceId = userOrder.getDeviceId();
//                    String deviceType = userOrder.getDeviceType();
//                    String payType = userOrder.getPayType();
//                    String payAmount = userOrder.getAmount();
//
//                    //请求body参数处理
//                    PayNotifyRequestBody body = new PayNotifyRequestBody();
//                    body.setOrderId(orderId);
//                    body.setDeviceId(deviceId);
//                    body.setDeviceType(deviceType);
//                    body.setPayType(payType);
//                    body.setPayAmount(payAmount);
//
//                    //参数拼接
//                    PayNotifyRequest request = new PayNotifyRequest();
//                    TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
//                    RequestHeader header = utils.getRequestHeader(Constant.cmd_payNotify, partnerCode);
//                    request.setHeader(header);
//                    request.setBody(body);
//                    String sign = utils.getSign(header, body, body.getClass(), secret);
//
//                    request.setSign(sign);
//                    // 将参数转换成XML格式
//                    String sRequest = new JaxbUtil(PayNotifyRequest.class).toXml(request, "UTF-8");
//                    String url = "http://localhost:80/TestService/test/payNotify";
//                    // 发送post请求，并接收返回结果
//                    String result = HttpClientTool.sendHttpPost(url, sRequest);
//
//                    PayNotifyResponse responseObj = new JaxbUtil(PayNotifyResponse.class).fromXml(result, "utf-8");
//                    // 返回错误结果的处理
//                    String retCode = responseObj.getHeader().getResultCode();
//                    if (false == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
//                        System.out.println("同步支付结果错误");
//                    } else {
//                        // 所有的通知结束之后，将数据库里面的isNotify变成0，即为不需要再次通知
//                        UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
//                        paymentOrder.setIsNotify("0");
//                        paymentOrder.setOrderId(orderId);
//                        userboundPaymentOrderService.updateIsNotify(paymentOrder);
//                        // 当程序跑到这一步就真的完成了
//                        System.out.println("同步支付结果成功");
//                        System.out.println("NotificationNoDeviceTask微信无卡用户批量支付结果通知定时任务执行结束！");
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}
